package com.example.demo.controller;

import com.example.demo.entity.UserInfo;
import com.example.demo.service.LogService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private LogService logService;
    @Autowired
    private UserService userService;
    @Autowired
    private DataSourceTransactionManager transactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;
    @RequestMapping("/add")
    public int add(UserInfo userInfo){
        if(userInfo==null || !StringUtils.hasLength(userInfo.getUsername())
        || !StringUtils.hasLength(userInfo.getPassword())){
            return 0;
        }
        //todo: 非空校验
        //1.开始事务
        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
        //手动设置创建时间和修改时间
        userInfo.setCreatetime(LocalDateTime.now().toString());
        userInfo.setUpdatetime(LocalDateTime.now().toString());

        int result=userService.add(userInfo);
        System.out.println("添加"+result);

        //2. 回滚事务
        transactionManager.rollback(transactionStatus);

        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)// 声明式事务，自动提交
    @RequestMapping("/insert")
    public Integer insert(UserInfo userInfo){
        //非空校验
        if(userInfo==null || !StringUtils.hasLength(userInfo.getUsername())
                || !StringUtils.hasLength(userInfo.getPassword())){
            return 0;
        }
        int result=userService.add(userInfo);
        if(result>0){
            //添加日志
            logService.add();
        }
        System.out.println("添加insert"+result);
        try {
            int num = 10 / 0;
        }catch (Exception e){
            //1.将异常继续抛出
            //throw e;
            //2.使用代码进行手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }
}
